package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-07-12 14:17
 */
@RequestMapping("/admin/product")
@RestController
public class BaseAttrRestController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;


    /**
     * 查询某个分类下所有的平台属性名和值
     * @param c1Id
     * @param c2Id
     * @param c3Id
     * @return
     */
    @GetMapping("/attrInfoList/{c1Id}/{c2Id}/{c3Id}")
    public Result getattrInfoList(@PathVariable("c1Id") Long c1Id,
                                  @PathVariable("c2Id") Long c2Id,
                                  @PathVariable("c3Id") Long c3Id){

        //甩锅式代码
        //查询某个分类下所有的平台属性名和值
        List<BaseAttrInfo> infos = baseAttrInfoService.getAttrInfoAndValueList(c1Id,c2Id,c3Id);

        return Result.ok(infos);
    }

    /**
     * 保存/修改平台属性 二合一
     * @param info
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo info){
        if(info.getId() == null){
            //这种情况是保存
            baseAttrInfoService.saveAttrInfo(info);
        }else {
            //修改
            baseAttrInfoService.updateAttrInfo(info);
        }

        return Result.ok();
    }

    /**
     * 根据 平台属性id 获取属性值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){

        List<BaseAttrValue> attrValues = baseAttrValueService.getAttrValueList(attrId);

        return Result.ok(attrValues);
    }
}
